//
//  Train. 冒泡排序.swift
//  LeetCodeTrain
//
//  Created by rjb on 2021/7/18.
//  Copyright © 2021 rjb. All rights reserved.
//

import Foundation
/**
 左边是无序的，右边是有序的；
 冒泡排序实际上是两两比较，右边的最终会变成有序
 */
class SolutionMaopaoSortTrain {
    func maopaoSort(_ nums: inout [Int]) {
        for i in 0..<nums.count {
            // 每经过一次，后面的最大元素都有序了
            for j in 0..<(nums.count - i - 1) {
                // 逆序时，两两交换
                if nums[j] > nums[j + 1] {
                    let tmp = nums[j + 1]
                    nums[j + 1] = nums[j]
                    nums[j] = tmp
                }
            }
        }
    }
    static func test() {
        var nums = [156,141,35,94,88,61,111]
        let solution = SolutionMaopaoSortTrain()
        solution.maopaoSort(&nums)
        print(nums)
    }
}
